<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2018-02-08 18:04:09
 */
if (!defined('IN_DISCUZ')) {
    exit('Access Denid');
}
class table_jamesonread_colums extends discuz_table
{
    public function __construct()
    {
        $this->_table         = 'jamesonread_colums';
        $this->_pk            = 'colum_id';
        $this->_pre_cache_key = 'jamesonreadcolums_';
        $this->_cache_ttl     = 300;
        parent::__construct();
    }
    public function isColum($tid)
    {
        if ($cache = $this->fetch_cache('is' . $tid)) {
            return $cache;
        }
        $is = (int) DB::result_first('SELECT count(colum_id) FROM %t WHERE tid=%d', array($this->_table, $tid));
        $this->store_cache('is' . $tid, $is);
        return $is;
    }
    public function fetch_by_bid($book_id)
    {
        return (int) DB::result_first('SELECT zhangjie FROM %t WHERE book_id=%d ORDER BY zhangjie DESC,addtime DESC LIMIT 1', array($this->_table, $book_id));
        re_cache($book_id . '_zhangjie', $data);

    }
    public function fetch_bytid($tid)
    {
        if ($cache = $this->fetch_cache('bytid' . $tid)) {
            return $cache;
        }
        $data = DB::fetch_first('SELECT c.*,b.book_name,b.author FROM %t as c INNER JOIN %t as b ON c.book_id=b.book_id WHERE c.tid=%d LIMIT 1', array($this->_table, 'jamesonread_books', $tid));
        if ($data) {
            $this->store_cache('bytid' . $tid, $data);
        }
        return $data;
    }
    public function fetch_cid($tid)
    {
        if ($cache = $this->fetch_cache('cid' . $tid)) {
            return $cache;
        }
        $data = DB::result_first('SELECT colum_id FROM %t WHERE tid=%d', array($this->_table, $tid));
        if ($data) {
            $this->store_cache('cid' . $tid, $data);
        }
        return $data;
    }
    public function fetch_pid_bytid($tid)
    {
        if ($cache = $this->fetch_cache('pidbytid' . $tid)) {
            return $cache;
        }
        $data = C::t('forum_post')->fetch_all_by_tid_position('tid:'.$tid, $tid, 1);
        $data = (int) $data[0]['pid'];
        if ($data) {
            $this->store_cache('pidbytid' . $tid, $data);
        }
        return $data;
    }
    public function fetch_colum_bytid($tid)
    {
        return DB::fetch_first('SELECT * FROM %t WHERE tid=%d', array($this->_table, $tid));
    }
    public function fetch_allmulu($book_id)
    {
        if ($cache = $this->fetch_cache('mulu' . $book_id)) {
            return $cache;
        }
        $fenjuan = (array) DB::fetch_all('SELECT * FROM %t WHERE book_id=%d ORDER BY order_num ASC', array('jamesonread_fenjuan', $book_id), 'j_id');
        $data    = array();
        if ($fenjuan) {
            $nofor = DB::fetch_all('SELECT * FROM %t WHERE ' . DB::field('j_id', array_keys($fenjuan), 'in') . ' ORDER BY j_id ASC, zhangjie ASC,addtime ASC', array('jamesonread_colums'));
            foreach ($nofor as $key => $value) {
                if ($fenjuan[$value['j_id']]) {
                    // 以colum_id作为key，非常重要
                    $fenjuan[$value['j_id']]['colums'][$value['colum_id']] = $value;
                }
            }
            foreach ($fenjuan as $key => $value) {
                foreach ($value['colums'] as $key2 => $value2) {
                    $data[$value2['colum_id']] = $value2;

                }
            }
        }
        $this->store_cache('mulu' . $book_id, $data);
        return $data;
    }
    public function precolum($book_id, $zhangjie, $colum_id)
    {
        if (!$colum_id) {
            return array();
        }
        $data = $this->fetch_allmulu($book_id);
        if (!$data) {
            return array();
        }
        $allcolumid = array_keys($data); //所有的colum_id
        $currentkey = array_search($colum_id, $allcolumid);
        // 获取当前colum_id在键数组中的位置
        if (false !== $currentkey) {
            return ($currentkey < 1) ? array() : $data[$allcolumid[$currentkey - 1]];
        }
        return array();
    }
    public function nextcolum($book_id, $zhangjie, $colum_id)
    {
        if (!$colum_id) {
            return array();
        }
        $data = $this->fetch_allmulu($book_id);
        if (!$data) {
            return array();
        }
        $allcolumid = array_keys($data); //所有的colum_id
        $currentkey = array_search($colum_id, $allcolumid);
        // 获取当前colum_id在键数组中的位置
        if (false !== $currentkey) {
            return ($currentkey > count($allcolumid) - 2) ? array() : $data[$allcolumid[$currentkey + 1]];
        }
        return array();
    }
    public function countAll()
    {
        return (int) DB::result_first('SELECT count(colum_id) FROM %t', array($this->_table));
    }
    public function fetch_newcolum_bybid($book_id)
    {
        $new = DB::fetch_all('SELECT * FROM %t  WHERE book_id=%d  ORDER BY zhangjie DESC,addtime DESC LIMIT %d', array($this->_table, $book_id, 1));
        if(!$new){
            return array();
        }
        $col = C::t("forum_post")->fetch('tid:' . $new[0]['tid'], $new[0]['pid']);
        $new[0]['message'] = $col['message'];
        return $new;
    }
    // 没有生成图片的章节数量
    function count_noimg(){
        $data = (array) DB::fetch_all('SELECT c.tid,c.pid,c.subject,c.book_id,b.book_name,b.author FROM %t as c INNER JOIN %t as b ON c.book_id=b.book_id ORDER BY c.colum_id DESC',array($this->_table,'jamesonread_books'));
        foreach ($data as $key => $value) {
            if(is_file(DISCUZ_ROOT.'/data/attachment/forum/colums/book_'.$value['book_id'].'_'.$value['tid'].'.jpg')){
                unset($data[$key]);
            }
        }
        return $data;
    }
}